/**
 * 
 */
package com.dangdang.serviceImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.dangdang.domain.Item;
import com.dangdang.service.ItemService;
import com.dangdang.util.HbnUtils;

/**
 * 订单详细条目管理实现类
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-19
 */
public class ItemServiceImpl implements ItemService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dangdang.service.ItemService#addItem(com.dangdang.domain.Item)
	 */
	@Override
	public void addItem(Item item) {
		Session s = HbnUtils.getSession();
		try {
			s.beginTransaction();
			s.save(item);
			s.getTransaction().commit();
		} catch (Exception e) {
			s.getTransaction().rollback();
		} finally {
			s.close();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.dangdang.service.ItemService#deleteItem(com.dangdang.domain.Item)
	 */
	@Override
	public void deleteItemById(Integer itemId) {
		Session s = HbnUtils.getSession();
		try {
			s.beginTransaction();
			s.delete(itemId);
			s.getTransaction().commit();
		} catch (Exception e) {
			s.getTransaction().rollback();
		} finally {
			s.close();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dangdang.service.ItemService#findAllItem()
	 */
	@Override
	public Item findAllItemById(Integer itemId) {
		Session s = HbnUtils.getSession();
		String hql = "from Item where Id = '" + itemId + "'";
		Query query = s.createQuery(hql);
		Item item = (Item) query.uniqueResult();
		s.close();
		return item;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dangdang.service.ItemService#findItemById(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Item> findItemByOrderId(Integer orderId) {
		Session s = HbnUtils.getSession();
		String hql = "from Item where orderId = '" + orderId + "'";
		Query query = s.createQuery(hql);
		List<Item> itemsList = query.list();
		s.close();
		return itemsList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.dangdang.service.ItemService#updateItem(com.dangdang.domain.Item)
	 */
	@Override
	public void updateItem(Item item) {
		Session s = HbnUtils.getSession();
		try {
			s.beginTransaction();
			s.update(item);
			s.getTransaction().commit();
		} catch (Exception e) {
			s.getTransaction().rollback();
		} finally {
			s.close();
		}

	}

}
